package com.sdwz.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdwz.commonutil.Result;
import com.sdwz.user.entity.BorrowCardInfo;
import com.sdwz.user.entity.UserInfo;
import com.sdwz.user.entity.vo.BorrowCardInfoVo;
import com.sdwz.user.entity.vo.UserInfoVo;
import com.sdwz.user.service.BorrowCardInfoService;
import com.sdwz.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-26
 */
@RestController
@RequestMapping("/admin/user")
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;

    @Autowired
    BorrowCardInfoService borrowCardInfoService;

    @GetMapping("/getUser/{userId}")
    public UserInfo getUser(@PathVariable String userId){
        return userInfoService.getById(userId);
    }

    /**
     * 多条件分页查询，只完成认证状态条件查询，可扩展
     * TODO
     * @param current
     * @param limit
     * @param borrowCardInfoVo
     * @return
     */
    @GetMapping("/list/{current}/{limit}")
    public Result getUserInfoList(@PathVariable String current,
                                  @PathVariable String limit,
                                  BorrowCardInfoVo borrowCardInfoVo){

       Map<String,Object> result =  userInfoService.getUserInfoList(Long.parseLong(current),Long.parseLong(limit),borrowCardInfoVo);
        System.out.println(result);
       return Result.ok(result);
    }

    //通过认证
    @PostMapping("/authSuccess/{userId}")
    public Result throughValidation(@PathVariable String userId){
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        userInfo.setStatus(2);
        userInfoService.updateById(userInfo);

        QueryWrapper<BorrowCardInfo> wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        BorrowCardInfo borrowCardInfo = borrowCardInfoService.getOne(wrapper);
        if (borrowCardInfo == null){
            return Result.fail().message("借阅卡不存在");
        }
        borrowCardInfo.setStatus(1);
        borrowCardInfo.setAuthStatus(2);
        borrowCardInfoService.updateById(borrowCardInfo);

        return Result.ok();
    }

    //不通过认证
    @PostMapping("/authFail/{userId}")
    public Result authFail(@PathVariable String userId){
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        userInfo.setStatus(-1);
        userInfoService.updateById(userInfo);

        QueryWrapper<BorrowCardInfo> wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        BorrowCardInfo borrowCardInfo = borrowCardInfoService.getOne(wrapper);
        if (borrowCardInfo == null){
            return Result.fail().message("借阅卡不存在");
        }
        borrowCardInfo.setStatus(0);
        borrowCardInfo.setAuthStatus(-1);
        borrowCardInfoService.updateById(borrowCardInfo);

        return Result.ok();
    }
}

